package com.bit.talkoftoday;

import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bit.talkoftoday.ShakeDetectActivity;
import com.bit.talkoftoday.ShakeDetectActivityListener;
import com.bit.talkoftoday.R.drawable;

public class ShakeActivity extends Activity {

	ShakeDetectActivity shakeDetectActivity;
	private Vibrator vibrator;
	private int count;
	private RotateAnimation rotate;
	private ImageView ivbox;
	Animation animation;
	private RelativeLayout RL;
	private String bookName;
	private String contents;
	private Activity myActivity;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.box_1);
		
		shakeDetectActivity = new ShakeDetectActivity(this);
		shakeDetectActivity.addListener(new ShakeDetectActivityListener() {
			@Override
			public void shakeDetected() {
				ShakeActivity.this.triggerShakeDetected();
			}
		});

		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		myActivity = this;
	}

	@Override
	protected void onResume() {
		super.onResume();
		shakeDetectActivity.onResume();
	}

	@Override
	protected void onPause() {
		shakeDetectActivity.onPause();
		super.onPause();
	}

	public void triggerShakeDetected() {
		ivbox = (ImageView) findViewById(R.id.box1);
		if (count < 5) {
			count++;
		}
		switch (count) {
		case 1:
			ivbox.setImageResource(drawable.box2);
			rotate = new RotateAnimation(0, 3, Animation.RELATIVE_TO_PARENT,
					0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
			rotate.setDuration(40);
			rotate.setRepeatMode(Animation.REVERSE);
			rotate.setRepeatCount(-1);

			ivbox.startAnimation(rotate);
			vibrator.vibrate(new long[]{100,40},0);
			break;
		case 2:
			ivbox.setImageResource(drawable.box3);
			rotate = new RotateAnimation(0, 5, Animation.RELATIVE_TO_PARENT,
					0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
			rotate.setDuration(30);
			rotate.setRepeatMode(Animation.REVERSE);
			rotate.setRepeatCount(-1);

			ivbox.startAnimation(rotate);
			vibrator.vibrate(new long[]{100,30},0);
			break;
		case 3:
			ivbox.setImageResource(drawable.box4);
			rotate = new RotateAnimation(0, 7, Animation.RELATIVE_TO_PARENT,
					0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
			rotate.setDuration(20);
			rotate.setRepeatMode(Animation.REVERSE);
			rotate.setRepeatCount(-1);

			ivbox.startAnimation(rotate);
			vibrator.vibrate(new long[]{100,20},0);
			break;

		case 4:
			AlphaAnimation alpha=new AlphaAnimation(1.0f, 0.0f);
			RL=(RelativeLayout)findViewById(R.id.RL);
			alpha.setDuration(1000);
			ivbox.setImageResource(drawable.box5);
			rotate = new RotateAnimation(0, 0,
					RotateAnimation.RELATIVE_TO_SELF, 0.5f,
					RotateAnimation.RELATIVE_TO_SELF, 0.5f);
			ivbox.startAnimation(rotate);
			ivbox.startAnimation(alpha);
			RL.startAnimation(alpha);
			alpha.setFillAfter(true);
			alpha.setFillEnabled(true);
			vibrator.vibrate(1000);
			readX();
			ImageCreate i = new ImageCreate();
			i.start();
			break;
		}
	}
	class ImageCreate extends Thread{
		@Override
		public void run() {
			try {
				sleep(1000);
				Intent intent = new Intent(ShakeActivity.this,CardActivity.class);
				intent.putExtra("bookName", bookName);
				intent.putExtra("contents", contents);
				startActivity(intent);
				myActivity.finish();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void readX(){
		HSSFWorkbook wb;
		HSSFSheet sheet;
		HSSFRow row;
		try{
			InputStream fis=getBaseContext().getResources().getAssets().open("SelectData.xls");
			wb=new HSSFWorkbook(fis);
			fis.close();
			sheet=wb.getSheetAt(0);
			if(wb!=null&&sheet!=null){
				int srow=sheet.getFirstRowNum()+1;
				int erow=sheet.getLastRowNum();
				row=sheet.getRow(0);
				int scol=row.getFirstCellNum();
				int ecol=row.getLastCellNum();
				int random=(int)(Math.random()*(erow-srow+1))+srow;
				HSSFRow tmp;
				HSSFCell cell;
				tmp= sheet.getRow(random);
				for(int i=scol; i<ecol; i++){
					cell= tmp.getCell(i);
					if(i==scol){
						bookName=cell.getStringCellValue();
					}else{
						contents=cell.getStringCellValue();
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			finish();
			vibrator.cancel();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		vibrator.cancel();
	}
	
	
}
